package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

/**
 *  GxNewsAnalyse  广西新闻网新闻解析类
 *  xiaonie 2012-11-23
 */
public class GxNewsAnalyse implements AnalyseNews{
	private final String[] regexs={
			"http://[a-z]*[.]gxnews.com.cn/staticpages/[0-9]{8}/newgx.*-[0-9]+.shtml",
			"http://auto.gxnews.com.cn/news/story_[0-9]+.html",
			"http://yy.gxnews.com.cn/html/[a-z]*/[a-z]*/[0-9]+.html"};

	
	public boolean isDetailPage(String url) {
		for (String regex : regexs) {
			if (url.matches(regex)) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		Document doc=Jsoup.parse(htmltxt);
		String title=null;
		String content=null;
		Long date=null;
		String dateStr=null;

		doc.select(".page").remove();
		doc.select(".a_tj").remove();
		doc.select(".tj_td").remove();

		if (htmltxt != null && htmltxt != "") {
			news.setUrl(url);
			if(url.startsWith("http://house.gxnews.com.cn/")){
				title=doc.select("td.biaoti01").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td#article_content"));
				dateStr=doc.select("td.neiwen").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://auto.gxnews.com.cn/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.cs_content"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.ina_news_contents"));
				}
				dateStr=doc.select("div.cs_title > p").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://health.gxnews.com.cn/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.neiwen3.article_content"));
				dateStr=doc.select("td.neiwen").text();
				if(title == null | title.trim().length() == 0){
					title=doc.select("div#left_1").text();
				}
				if(content == null | content.trim().length() == 0){
					content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));
				}
				if(dateStr == null | dateStr.trim().length() == 0){
					dateStr=doc.select("div#left_2").text();
				}
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://yy.gxnews.com.cn/")){
				title=doc.select("h1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div.show_left_content"));
				dateStr=doc.select("div.show_left_title>span").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://wzhd.gxnews.com.cn/")){
				title=doc.select("td.dabiao").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.kuang1 table tbody tr:eq(6)"));
				dateStr=doc.select("td.tushuo").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://bs.gxnews.com.cn/")){
				title=doc.select("td.cBlue h3").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("td.fs14"));
				dateStr=doc.select("td.cBlue").text();
				date=DateUtils.matchDate(dateStr);
			}else if(url.startsWith("http://culture.gxnews.com.cn/")){
				try {
					title=doc.select("h3").text();
					content=HtmlCleaner.getContentHtml(url,doc.select("p[style=TEXT-INDENT: 2em]"));
					Elements select=doc.select("table[width=644]");
					Element element=select.get(0);
					dateStr=element.select("td[valign=top]").text();
					date=DateUtils.matchDate(dateStr);
				} catch (Exception e) {
				}
			}else{
				title=doc.select("div#left_1").text();
				content=HtmlCleaner.getContentHtml(url,doc.select("div#content p"));
				dateStr=doc.select("div#left_2").text();
				date=DateUtils.matchDate(dateStr);
			}
			if(StringUtils.isBlank(title)){
				title=doc.select("td[height=30]").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("td.title").text();
				}
			}
			if (date==null){
				date=DateUtils.matchDate(doc.select("span.info span.r").text());
				if (date==null){
					date=DateUtils.matchDate(doc.select("td[height=25]").text());
					if (date==null){
						date=DateUtils.matchDate(doc.select("div.ina_news_text").text());
					}
				}
			}
			if (date==null){
				date=DateUtils.matchDate(url);
			}
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("td.article_content p"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("html body center div table tbody tr td p"));
				}
			}

			if(StringUtils.isBlank(title)){
				title=doc.select(".article h1").text();
			}
			if(StringUtils.isBlank(content)){
				content=doc.select(".article-content").html();
			}

			news.setContent(content);
			news.setTitle(title);
			news.setClickNum(null);
			news.setCommentNum(null);
			news.setDate(date);
			news.setType(1);

			String author= JSoupUtils.matchAuthor(doc, "来源:");
			news.setAuthor(author);
		}
		return news;
	}

	public static void main(String[] args) {
		GxNewsAnalyse a=new GxNewsAnalyse();
		String url="http://auto.gxnews.com.cn/staticpages/20160627/newgx5770fd72-15136667.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(a.isDetailPage(url));
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
